package com.gotrade.demo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @author jason.tang
 * @create 2018-11-28 11:31
 * @description
 */
public class NettyServer {

    public static void main(String[] args) {

        // 服务初始化启动器
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        // 处理连接的线程池
        NioEventLoopGroup bossGrp = new NioEventLoopGroup();
        // 读取数据以及业务逻辑处理线程池
        NioEventLoopGroup wokerGrp = new NioEventLoopGroup();

        serverBootstrap.group(bossGrp, wokerGrp)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new SimpleChannelInboundHandler<String>() {
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                System.out.println(s);
                            }
                        });
                    }
                })
                .bind(8898);
    }
}